package com.menghao.sso.server.model;

import lombok.Getter;
import org.springframework.util.StringUtils;

public class SimpleService implements Service {

    private static final long serialVersionUID = 3258129150454936116L;

    @Getter
    private String url;

    public SimpleService(final String url) {

        if (url == null) {
            throw new IllegalArgumentException("ID cannot be null");
        }

        this.url = url;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SimpleService that = (SimpleService) o;
        return (StringUtils.hasText(that.getUrl()) && StringUtils.hasText(this.getUrl())) && getHost(this).equals(getHost(that));
    }


    private String getHost(SimpleService simpleService) {
        String url = simpleService.getUrl();
        url = url.substring(url.indexOf("//") + 2);
        url = url.substring(0, url.indexOf("/"));
        return url;
    }
}
